/*
 * Copyright (c) 2017,安徽皖通科技股份有限公司 All rights reserved.
 */

package com.wttech.dzzf.process.service.govern;

import com.wtkj.vo.RandomGUID;
import com.wtkj.vo.dzzf.base.kfwd.CardInfo;
import com.wtkj.vo.dzzf.base.kfwd.OBU;
import com.wtkj.vo.dzzf.base.kfwd.VehicleInfo;
import com.wttech.dzzf.process.dao.DataGovernDao;
import com.wttech.dzzf.process.dao.TransferUserInfoDao;
import com.wttech.dzzf.process.util.BusinessUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.wttech.dzzf.check.moduleSet.CheckCardUpload_10.*;
import static com.wttech.dzzf.process.constant.UserConstants.*;

/**
 * 卡片信息治理服务实现
 *
 * @author MaShibo
 * @date 2017/12/7.
 */
@Service
class CardInfoGovernService {

	private static final Logger LOGGER = LoggerFactory.getLogger(CardInfoGovernService.class);

	private static final Logger CARD = LoggerFactory.getLogger("CARD");


	@Resource
	private TransferUserInfoDao transferUserInfoDao;

	@Resource
	private DataGovernDao dataGovernDao;

	@Resource
	private VehicleInfoGovernService vehicleInfoGovernService;

	@Resource
	private ObuInfoGovernService obuInfoGovernService;

	void governCardInfo(CardInfo cardInfo) {
		String cardInfoId = cardInfo.getCardInfoId();

		String result;

		String cardId = cardInfo.getCardNetID() + cardInfo.getECardNo();
		Integer brand = cardInfo.getCardBrand();
		String model = cardInfo.getCardModel();
		String vehicleId = cardInfo.getVehiclePlate() + DEFAULT_VEHICLE_CHAR + cardInfo.getVehicleColor();

		CardInfo update = new CardInfo();
		update.setCardInfoId(cardInfoId);
		boolean isUpdate = false;

		result = checkId(cardId);
		if (!StringUtils.equalsIgnoreCase(result, RIGHT)) {
			CARD.info("卡片信息【{}】，卡片编号【{}】错误【{}】", new Object[]{cardInfoId, cardId, result});
		}

		result = checkBrand(brand);
		if (!StringUtils.equalsIgnoreCase(result, RIGHT)) {
			update.setCardBrand(BusinessUtils.determineCardBrand(cardId, cardInfo.getECardType(), cardInfo.getTopClass()));
			isUpdate = true;
		}

		result = checkModel(model);
		if (!StringUtils.equalsIgnoreCase(result, RIGHT)) {
			update.setCardModel(DEFAULT_CARDMODEL);
			isUpdate = true;
		}

		result = checkVehicleId(vehicleId);
		boolean rightPlate = StringUtils.equalsIgnoreCase(result, RIGHT);

		if (isUpdate) {
			transferUserInfoDao.updateDzzfObject(update);
		}

		//车牌号
		String queryPlate = rightPlate ? cardInfo.getVehiclePlate() : DEFAULT_VEHICLE_PLATE;
		VehicleInfo vehicleInfo = dataGovernDao.queryVehInfosByVeh(queryPlate, cardInfo.getVehicleColor());
		if (vehicleInfo == null) {
			createVehicleInfo(cardInfo, queryPlate);
		} else {
			vehicleInfoGovernService.governVehicleInfo(vehicleInfo);
		}

		//OBU
		List<OBU> obus = dataGovernDao.queryObuInfosByVeh(cardInfo.getVehiclePlate(), cardInfo.getVehicleColor());
		obus.forEach(obu -> {
			try {
				obuInfoGovernService.governObu(obu);
			} catch (Exception e) {
				LOGGER.error(e.getMessage(), e);
			}
		});
	}

	/**
	 * 创建车辆信息
	 *
	 * @param cardInfo 卡片信息
	 * @param vehPlate 车牌号
	 */
	private void createVehicleInfo(CardInfo cardInfo, String vehPlate) {

		VehicleInfo vehicleInfo = new VehicleInfo();
		vehicleInfo.setVehicleInfoId(new RandomGUID().toString());
		vehicleInfo.setVehPlate(vehPlate);
		vehicleInfo.setVehColor(cardInfo.getVehicleColor());
		vehicleInfo.setVehType(cardInfo.getVehicleType());
		vehicleInfo.setContact(DEFAULT_CONTACT);
		vehicleInfo.setVehiclelicNO(DEFAULT_VEHICLE_VIN);
		transferUserInfoDao.createTestObjcet(vehicleInfo);
		LOGGER.info("创建车辆信息【{}】, 车牌号【{}】", vehicleInfo.getVehicleInfoId(), vehPlate);
	}
}
